<?php
namespace app\api\controller\kuang;
use app\api\controller\Common;
use think\Db;
class Caiwu extends Common{	//个人中心 需验证登陆
	public function _empty($name){
        die('非法访问！你的IP已被监控！');
    }
    public function mingxi_do(){    //财务明细
        /*
        var postdata = { //请求数据默认值
            zichan: 'OME',
            yongtu: '全部',
            date: today,
            type: 'shuaxin', //shuaxin=重新加载，loadmore=上拉加载更多
            count: '0', //已加载的条数，默认0，加载完返回条数，type=loadmore时传入条数
            keywords: '' //关键字keywords存在 则zichan/yongtu/date无效
        };
        */
        $appdata = $this->appdata;      //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;      //系统参数
        //表判断
        if($appdata['zichan']=='OME'){
            $table='logome';
        }elseif($appdata['zichan']=='矿金'){
            $table='logkuang';
        }elseif($appdata['zichan']=='M币'){
            $table='logquan';
        }elseif($appdata['zichan']=='购物积分'){
            $table='logjifen';
        }elseif($appdata['zichan']=='分享社区'){
            $table='logdkuang';
        }else{
            apifasong(['zt'=>'0','msg'=>'获取失败']);
        }
        //条件 判断
        $where['uname']=$userinfo['uname'];
        if($appdata['keywords']){
            //条件 关键词
            $where['note|oname']=['like','%'.$appdata['keywords'].'%'];
        }else{
            //时间 筛选
            $where['time']=['ELT',strtotime($appdata['date'].' 23:59:59')];
            //类型 筛选
            if($table<>'logdkuang'){
                if($appdata['yongtu']=='转出'){
                    $where['type']='zhuan';
                    $where['typeb']='chu';
                }elseif($appdata['yongtu']=='转入'){
                    $where['type']='zhuan';
                    $where['typeb']='jin';
                }elseif($appdata['yongtu']=='提链'){
                    $where['type']='tilian';
                }elseif($appdata['yongtu']=='手续费'){
                    $where['type']='sxf';
                }elseif($appdata['yongtu']=='购买矿机'){
                    $where['type']='mkj';
                }elseif($appdata['yongtu']=='升级矿机'){
                    $where['type']='skj';
                }elseif($appdata['yongtu']=='静态收益'){
                    $where['type']='jing';
                }elseif($appdata['yongtu']=='动态收益'){
                    $where['type']='dong';
                }
            }
        }
        if($appdata['type']=='loadmore' && $appdata['count']){
            $limit1=$appdata['count'];  //从已加载条数开始
        }elseif(true || $appdata['type']=='shuaxin'){
            $limit1=0;  //从0开始
        }
        
        $limit2=10;    //取100条
        $lists=db($table)->where($where)->limit($limit1,$limit2)->order('id desc')->select();
        if($lists){
            apifasong(['zt'=>'1','lists'=>$lists,'count'=>$limit1+$limit2]);
        }else{
            if($appdata['type']=='loadmore' && $appdata['count']){
                $msg='没有更多了';
            }elseif(true || $appdata['type']=='shuaxin'){
                $msg='没有数据';
            }
            apifasong(['zt'=>'0','msg'=>$msg,'lists'=>'']);
        }
    }
    public function zhuanzhang_do(){    //转账提交
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        
        //必填项检测
        if(!isset($appdata['toname']) || !isset($appdata['secpwd'])){
            apifasong(['zt'=>'-2','msg'=>'非法请求']);
        }
        if(!isset($appdata['ome']) && !isset($appdata['kuang']) && !isset($appdata['quan'])){
            apifasong(['zt'=>'-1','msg'=>'请输入转账额度']);
        }
        //用户名检测
        $touserinfo=userinfobyuname($appdata['toname']);
        if(!$touserinfo){
            apifasong(['zt'=>'0','msg'=>'转账对象不存在']);
        }elseif($touserinfo['zt']<>1){
            apifasong(['zt'=>'0','msg'=>'转账对象账号异常']);
        }
        //二级密码验证
        $userinfo['secpwd']=$userinfo['secpwd']?$userinfo['secpwd']:MD5($setting['nopwd']);
        if(MD5($appdata['secpwd'])<>$userinfo['secpwd']){
            apifasong(['zt'=>'0','msg'=>'支付密码错误']);
        }
        //开始转账
        Db::startTrans();   // 启动事务-------------
        $position=chickusposition($touserinfo,$userinfo);//tou在u的位置
        if($appdata['ome']){
            //余额检测
            if($appdata['ome']>$userinfo['ome']){
                Db::rollback();     // 回滚事务-------------
                apifasong(['zt'=>'0','msg'=>'OME余额不足']);
            }
            //转账方向检测 可上下转，不可左右转
            if($position=='pang'){
                Db::rollback();     // 回滚事务-------------
                apifasong(['zt'=>'0','msg'=>'OME不可左右转']);
            }elseif($position=='shang'){
                if(!db('kj')->where('uname',$userinfo['uname'])->find()){
                    Db::rollback();     // 回滚事务-------------
                    apifasong(['zt'=>'0','msg'=>'未激活矿机OME不可向上转']);
                }
            }
            $ret1_ome1=ome1($userinfo,'-'.$appdata['ome'],'zhuan');   //可提链OME1处理
            $ret1_ome=db('user')->where('id',$userinfo['id'])->setDec('ome',$appdata['ome']);  //-款
            $logid1_ome=mylog('ome',$userinfo['uname'],'zhuan','chu',$userinfo['ome'],'-'.$appdata['ome'],'OME转出',$touserinfo['id'],$touserinfo['uname']);  //记录
            $ret2_ome1=ome1($touserinfo,$appdata['ome'],'zhuan');   //可提链OME1处理
            $ret2_ome=db('user')->where('id',$touserinfo['id'])->setInc('ome',$appdata['ome']);  //+款
            $logid2_ome=mylog('ome',$touserinfo['uname'],'zhuan','jin',$touserinfo['ome'],$appdata['ome'],'OME转入',$userinfo['id'],$userinfo['uname']);  //记录
            if(!$ret1_ome1 || !$ret1_ome || !$logid1_ome || !$ret2_ome1 || !$ret2_ome || !$logid2_ome){
                Db::rollback();     // 回滚事务-------------
                apifasong(['zt'=>'0','msg'=>'转账失败']);
            }
        }
        if($appdata['kuang']){
            //余额检测
            if($appdata['kuang']>$userinfo['kuang']){
                Db::rollback();     // 回滚事务-------------
                apifasong(['zt'=>'0','msg'=>'矿金余额不足']);
            }
            //转账方向检测 可上下转，不可左右转
            if($position=='pang'){
                Db::rollback();     // 回滚事务-------------
                apifasong(['zt'=>'0','msg'=>'矿金不可左右转']);
            }elseif($position=='shang'){
                Db::rollback();     // 回滚事务-------------
                apifasong(['zt'=>'0','msg'=>'矿金不可往上转']);
            }
            $ret1_kuang=db('user')->where('id',$userinfo['id'])->setDec('kuang',$appdata['kuang']);  //扣款
            $log1_kuang=mylog('kuang',$userinfo['uname'],'zhuan','chu',$userinfo['kuang'],'-'.$appdata['kuang'],'矿金转出',$touserinfo['id'],$touserinfo['uname']);  //记录
            $ret2_kuang=db('user')->where('id',$touserinfo['id'])->setInc('kuang',$appdata['kuang']);  //扣款
            $log2_kuang=mylog('kuang',$touserinfo['uname'],'zhuan','jin',$touserinfo['kuang'],$appdata['kuang'],'矿金转入',$userinfo['id'],$userinfo['uname']);  //记录
            if(!$ret1_kuang || !$log1_kuang || !$ret2_kuang || !$log2_kuang){
                Db::rollback();     // 回滚事务-------------
                apifasong(['zt'=>'0','msg'=>'转账失败']);
            }
        }
        if($appdata['quan']){
            //余额检测
            if($appdata['quan']>$userinfo['quan']){
                Db::rollback();     // 回滚事务-------------
                apifasong(['zt'=>'0','msg'=>'M币余额不足']);
            }
            //转账方向检测 可上下转，不可左右转
            if($position=='pang'){
                Db::rollback();     // 回滚事务-------------
                apifasong(['zt'=>'0','msg'=>'M币不可左右转']);
            }elseif($position=='shang'){
                if(!db('kj')->where('uname',$userinfo['uname'])->find()){
                    Db::rollback();     // 回滚事务-------------
                    apifasong(['zt'=>'0','msg'=>'未激活矿机M币不可向上转']);
                }
            }
            $ret1_quan=db('user')->where('id',$userinfo['id'])->setDec('quan',$appdata['quan']);  //扣款
            $log1_quan=mylog('quan',$userinfo['uname'],'zhuan','chu',$userinfo['quan'],'-'.$appdata['quan'],'M币转出',$touserinfo['id'],$touserinfo['uname']);  //记录
            $ret2_quan=db('user')->where('id',$touserinfo['id'])->setInc('quan',$appdata['quan']);  //扣款
            $log2_quan=mylog('quan',$touserinfo['uname'],'zhuan','jin',$touserinfo['quan'],$appdata['quan'],'M币转入',$userinfo['id'],$userinfo['uname']);  //记录
            if(!$ret1_quan || !$log1_quan || !$ret2_quan || !$log2_quan){
                Db::rollback();     // 回滚事务-------------
                apifasong(['zt'=>'0','msg'=>'转账失败']);
            }
        }

        //成功
        Db::commit();       // 提交事务-------------
        apifasong(['zt'=>'1','msg'=>'转账成功','userinfo'=>apiuserbaseinfo($userinfo['uname'])]);
    }
    public function yajin(){    //押金页面信息
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        $lv = $userinfo['lv'];  //等级

        $num_okkj = db('kj')->where('uname',$userinfo['uname'])->where('zt','neq',2)->count();
        apifasong(['zt'=>'1','userinfo'=>apiuserbaseinfo($userinfo['uname']),'num_okkj'=>$num_okkj]);
    }
    public function yajin_do(){    //押金缴纳/提取提交
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        $lv = $userinfo['lv'];  //等级
        
        //必填项检测
        if(!isset($appdata['money']) || !isset($appdata['secpwd']) || !isset($appdata['type'])){
            apifasong(['zt'=>'-2','msg'=>'非法请求']);
        }

        //二级密码验证
        $userinfo['secpwd']=$userinfo['secpwd']?$userinfo['secpwd']:MD5($setting['nopwd']);
        if(MD5($appdata['secpwd'])<>$userinfo['secpwd']){
            apifasong(['zt'=>'0','msg'=>'支付密码错误']);
        }
        //等级验证
        if($lv<2){
            apifasong(['zt'=>'0','msg'=>'OM'.$lv.'级别无法开启动态奖金池']);
        }
        //金额过滤
        $appdata['money'] = abs(intval($appdata['money'])); //转整数的绝对值
        //业务
        if($appdata['type'] == 'jiaona'){
            //判断余额
            if($userinfo['ome']<$appdata['money']){
                apifasong(['zt'=>'0','msg'=>'OME余额不足']);
            }
            Db::startTrans();   // 启动事务-------------
            $ret_ome = db('user')->where('id',$userinfo['id'])->inc('yajin',$appdata['money'])->dec('ome',$appdata['money'])->update();
            $ret_ome1 = ome1($userinfo,'-'.$appdata['money'],'yajin');
            $log_ome = mylog('ome',$userinfo['uname'],'yajin','chu',$userinfo['ome'],'-'.$appdata['money'],"缴纳押金","","");
            if($ret_ome && $ret_ome1 && $log_ome){
                Db::commit();       // 提交事务-------------
                apifasong(['zt'=>'1','msg'=>'缴纳成功','userinfo'=>apiuserbaseinfo($userinfo['uname'])]);
            }else{
                Db::rollback();     // 回滚事务-------------
                apifasong(['zt'=>'0','msg'=>'缴纳失败']);
            }
        }else{  //$appdata['type'] == 'tiqu'
            if($userinfo['yajin']<$appdata['money']){
                apifasong(['zt'=>'0','msg'=>'押金不足']);
            }
            Db::startTrans();   // 启动事务-------------
            $ret_ome = db('user')->where('id',$userinfo['id'])->dec('yajin',$appdata['money'])->inc('ome',$appdata['money'])->update();
            $ret_ome1 = ome1($userinfo,$appdata['money'],'yajin');;
            $log_ome = mylog('ome',$userinfo['uname'],'yajin','jin',$userinfo['ome'],$appdata['money'],"提取押金","","");;
            if($ret_ome && $ret_ome1 && $log_ome){
                Db::commit();       // 提交事务-------------
                apifasong(['zt'=>'1','msg'=>'提取成功','userinfo'=>apiuserbaseinfo($userinfo['uname'])]);
            }else{
                Db::rollback();     // 回滚事务-------------
                apifasong(['zt'=>'0','msg'=>'提取失败']);
            }
        }
    }
    public function shouqu(){    //收取矿金 页面基本信息
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        $lv = $userinfo['lv'];  //等级
        //0=无法开启 2=押金不足 1=OK
        //等级验证
        if($lv<2){
            apifasong(['zt'=>'0','msg'=>'OM'.$lv.'级别无法开启动态奖金池']);
        }
        //押金验证-为0
        if (!$userinfo['yajin']) {
            apifasong(['zt'=>'2','msg'=>'请先缴纳相应押金']);
        }
        //验证矿机
        $num_okkj = db('kj')->where('uname',$userinfo['uname'])->where('zt','neq',2)->count();
        if(!$num_okkj){
            apifasong(['zt'=>'0','msg'=>'您还没有矿机在运行']);
        }
        //验证押金-不足
        if($userinfo['yajin'] < $num_okkj*$setting['d_yajin_lv'.$lv]){
            apifasong(['zt'=>'2','msg'=>'您的押金不足']);
        }
        //奖金池页面基本信息
        $data_chi = [];
        //运行中矿机数
        $data_chi['num_okkj'] = $num_okkj;
        //押金基数
        $data_chi['yajin_base'] = $setting['d_yajin_lv'.$lv];
        //应交押金
        $data_chi['yajin_yingjiao'] = $num_okkj*$setting['d_yajin_lv'.$lv];
        //总投资额
        $data_chi['sum_touzi'] = db('kj')->where('uname',$userinfo['uname'])->sum('price');
        //总容量
        $data_chi['size'] = $setting['d_chuju_lv'.$lv]*$data_chi['sum_touzi'];
        //已用容量
        // $data_chi['yiyong'] = db('logdkuang')->where('uid',$userinfo['id'])->where('money','GT',0)->sum('money');    //累计已发放动态..优化方案：此方法定期检测dhas是否正确
        $data_chi['dhas'] = $userinfo['dhas'];
        //池内矿金
        $data_chi['dkuang'] = $userinfo['dkuang'];

        apifasong(['zt'=>'1','data_chi'=>$data_chi]);
    }

    public function shouqu_do(){    //收取矿金/萃取矿金提交
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        $lv = $userinfo['lv'];  //等级
        
        //必填项检测
        if(!isset($appdata['money']) || !isset($appdata['secpwd']) || !isset($appdata['type'])){
            apifasong(['zt'=>'-2','msg'=>'非法请求']);
        }

        //二级密码验证
        $userinfo['secpwd']=$userinfo['secpwd']?$userinfo['secpwd']:MD5($setting['nopwd']);
        if(MD5($appdata['secpwd'])<>$userinfo['secpwd']){
            apifasong(['zt'=>'0','msg'=>'支付密码错误']);
        }

        //0=无法开启 2=押金不足 1=OK
        //等级验证
        if($lv<2){
            apifasong(['zt'=>'0','msg'=>'OM'.$lv.'级别无法开启动态奖金池']);
        }
        //押金验证-为0
        if (!$userinfo['yajin']) {
            apifasong(['zt'=>'2','msg'=>'请先缴纳相应押金']);
        }
        //验证矿机
        $num_okkj = db('kj')->where('uname',$userinfo['uname'])->where('zt','neq',2)->count();
        if(!$num_okkj){
            apifasong(['zt'=>'0','msg'=>'您还没有矿机在运行']);
        }
        //验证押金-不足
        if($userinfo['yajin'] < $num_okkj*$setting['d_yajin_lv'.$lv]){
            apifasong(['zt'=>'2','msg'=>'您的押金不足']);
        }

        //金额过滤
        $appdata['money'] = abs(intval($appdata['money'])); //转整数的绝对值
        //判断余额
        if($userinfo['dkuang']<$appdata['money']){
            apifasong(['zt'=>'0','msg'=>'奖金池余额不足']);
        }


        //业务
        if($appdata['type'] == 'cuiqu'){
            //验证能量值|萃取限制 ..?。。？未完成 系统参数未设置
            $m_ome = round($appdata['money']*0.1,2);
            $m_kuang = round($appdata['money']*0.9,2);
            Db::startTrans();   // 启动事务-------------
            $ret_user = db('user')->where('id',$userinfo['id'])
                ->inc('ome',$m_ome)->inc('kuang',$m_kuang)->dec('dkuang',$appdata['money'])->update();
            $ret_ome1 = ome1($userinfo,$m_ome,'cuiqu');
            $log_ome = mylog('ome',$userinfo['uname'],'dong','cui',$userinfo['ome'],$m_ome,"萃取矿金","","");
            $log_kuang = mylog('kuang',$userinfo['uname'],'dong','cui',$userinfo['kuang'],$m_kuang,"萃取矿金","","");
            $log_dkuang = db('logdkuang')->insertGetId([
                'uid'=>$userinfo['id'],
                'uname'=>$userinfo['uname'],
                'time'=>time(),
                'from'=>'',
                'jing'=>'',
                'lv'=>$userinfo['lv'],
                'dai'=>'',
                'mqian'=>$userinfo['dkuang'],
                'money'=>'-'.$appdata['money'],
                'mhou'=>$userinfo['dkuang']-$appdata['money'],
                'note'=>'萃取矿金',
            ]);

            if($ret_user && $ret_ome1 && $log_ome && $log_kuang && $log_dkuang){
                Db::commit();       // 提交事务-------------
                apifasong(['zt'=>'1','msg'=>'萃取成功','userinfo'=>apiuserbaseinfo($userinfo['uname'])]);
            }else{
                Db::rollback();     // 回滚事务-------------
                apifasong(['zt'=>'0','msg'=>'萃取失败']);
            }
        }else{  //$appdata['type'] == 'tiqu'
            Db::startTrans();   // 启动事务-------------
            $ret_user = db('user')->where('id',$userinfo['id'])
                ->inc('kuang',$appdata['money'])->dec('dkuang',$appdata['money'])->update();
            $log_kuang = mylog('kuang',$userinfo['uname'],'dong','cui',$userinfo['kuang'],$appdata['money'],"提取矿金","","");
            $log_dkuang = db('logdkuang')->insertGetId([
                'uid'=>$userinfo['id'],
                'uname'=>$userinfo['uname'],
                'time'=>time(),
                'from'=>'',
                'jing'=>'',
                'lv'=>$userinfo['lv'],
                'dai'=>'',
                'mqian'=>$userinfo['dkuang'],
                'money'=>'-'.$appdata['money'],
                'mhou'=>$userinfo['dkuang']-$appdata['money'],
                'note'=>'提取矿金',
            ]);

            if($ret_user && $log_kuang && $log_dkuang){
                Db::commit();       // 提交事务-------------
                apifasong(['zt'=>'1','msg'=>'提取成功','userinfo'=>apiuserbaseinfo($userinfo['uname'])]);
            }else{
                Db::rollback();     // 回滚事务-------------
                apifasong(['zt'=>'0','msg'=>'提取失败']);
            }
        }
    }
}






